<template>
  <view class="applications">
    <!-- 页面标题 -->
    <view class="applications__header">
      <text class="applications__title">我的申请</text>
    </view>
    
    <!-- 申请列表 -->
    <view v-if="applications.length > 0" class="applications__list">
      <RecruitApplicationItem 
        v-for="item in applications" 
        :key="item.id"
        :title="getRecruitTitle(item.recruitId)"
        :status="item.status"
        :create-time="item.createTime"
        :feedback="item.feedback"
        @click="goToDetail(item.id)"
      />
    </view>
    
    <!-- 空状态 -->
    <view v-else-if="!loading" class="applications__empty">
      <image class="applications__empty-image" src="https://placekitten.com/300/200" mode="aspectFit"></image>
      <text class="applications__empty-text">暂无申请记录</text>
      <button class="applications__empty-btn" @click="goToRecruitList">查看招聘</button>
    </view>
    
    <!-- 加载中 -->
    <view v-else class="applications__loading">
      <text>加载中...</text>
    </view>
  </view>
</template>

<script setup>
import { computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import RecruitApplicationItem from '@/components/recruit/RecruitApplicationItem.vue';

// 初始化store
const store = useStore();

// 计算属性：申请记录列表
const applications = computed(() => store.getters['recruit/getUserApplications']);

// 计算属性：加载状态
const loading = computed(() => store.getters['recruit/loading']);

// 页面加载时获取数据
onMounted(() => {
  store.dispatch('recruit/fetchUserApplications');
});

// 格式化日期
const formatDate = (timestamp) => {
  if (!timestamp) return '暂无';
  const date = new Date(timestamp);
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
};

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    0: '待审核',
    1: '已查看',
    2: '面试邀请',
    3: '已录用',
    4: '未录用'
  };
  return statusMap[status] || '未知状态';
};

// 获取状态样式类名
const getStatusClass = (status) => {
  const statusClassMap = {
    0: 'pending',
    1: 'viewed',
    2: 'interview',
    3: 'accepted',
    4: 'rejected'
  };
  return statusClassMap[status] || 'pending';
};

// 跳转到岗位详情
const goToDetail = (id) => {
  uni.navigateTo({
    url: `/pages/recruit/detail?id=${id}`
  });
};

// 跳转到招聘列表
const goToRecruitList = () => {
  uni.switchTab({
    url: '/pages/recruit/index'
  });
};

// 获取招聘标题
const getRecruitTitle = (id) => {
  // 从store中获取岗位信息
  const recruitItem = store.getters['recruit/getRecruitDetailById'](id);
  return recruitItem?.title || '未知岗位';
};
</script>

<style lang="scss" scoped>
.applications {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding-bottom: 20px;
  
  &__header {
    padding: 16px;
    background-color: #ffffff;
    margin-bottom: 10px;
  }
  
  &__title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    text-align: center;
  }
  
  &__list {
    padding: 12px;
  }
  
  &__empty, &__loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 400px;
    padding: 0 30px;
  }
  
  &__empty-image {
    width: 200px;
    height: 150px;
    margin-bottom: 20px;
  }
  
  &__empty-text {
    font-size: 16px;
    color: #999;
    margin-bottom: 20px;
  }
  
  &__empty-btn {
    height: 40px;
    border-radius: 20px;
    background-color: #1890ff;
    color: #ffffff;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 30px;
  }
  
  &__loading {
    color: #999;
    font-size: 14px;
  }
}
</style>
